//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Xml;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Drawing;
using OpenLS.Vml.Parsing;
using OpenLS.Vml.Serialization;

namespace OpenLS.Vml
{
    public class VmlDrawing: IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("xml"))
            {
                while (c.ReaderLocalName != null)
                {
                    switch (c.ReaderLocalName)
                    {
                        case ShapeLayout.xmlName:
                            Layout = c.ReadOptionalElement<ShapeLayout>(ShapeLayout.xmlName);
                            break;
                        case ShapeType.xmlName:
                            ShapeTypes.Add(c.ReadElement<ShapeType>());
                            break;
                   //     case Shape.xmlName:
                     //       Items.Add(c.ReadElement<Shape>());break;
                     //   case Group.xmlName:
                       //     Items.Add(c.ReadElement<Group>());
                         //   break;
                            default:
                            Items.Add(Group.ReadGroupItem(c));
                            break;
                    }
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("xml"))
            {
                c.DeclareNamespace(VmlConstants.VmlNamespace);
                c.DeclareNamespace(VmlConstants.ExcelNamespace);
                c.DeclareNamespace(VmlConstants.OfficeNamespace);
                c.WriteOptionalElement(Layout);
                foreach (ShapeType t in ShapeTypes)
                    c.WriteOptionalElement(t);
                foreach (var t in Items)
                    c.WriteOptionalElement(t);
            }
        }

        #endregion

        private ShapeLayout _layout;

        public ShapeLayout Layout
        {
            get { return _layout; }
            set { _layout = value; }
        }
        private readonly List<ShapeType> _shapeTypes = new List<ShapeType>();

        public List<ShapeType> ShapeTypes
        {
            get { return _shapeTypes; }
        }
        private readonly List<IOfficeXmlSerializable> _items = new List<IOfficeXmlSerializable>();

        public List<IOfficeXmlSerializable> Items
        {
            get { return _items; }
        }


        public void ReadFromParentXml(ReadContext parentContext)
        {
            string id = parentContext.GetString(OfficeConstants.RelationshipNamespace + "id");
            PackageRelationship relationship = parentContext.Part.GetRelationship(id);
            PackagePart part = parentContext.Part.Package.GetPart(PackUriHelper.ResolvePartUri(relationship.SourceUri, relationship.TargetUri));
            using (Stream stream = part.GetStream())
            {
                using (TextReader textReader = new StreamReader(stream))
                {
#if NOSGML
                    using (XmlReader reader = new XmlTextReader(textReader))
#else
                    using (XmlReader reader= new SgmlReader(textReader))
#endif
                    {
                        using (
                            RelatedReadContext context = new RelatedReadContext(reader, part, "", stream, relationship))
                        {
                            context.CopyDataFrom(parentContext);
                            ((IOfficeXmlSerializable) this).ReadXml(context);
                        }
                    }
                }
            }
        }
    }
}
